package com.herench.bs.transfer;

import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.*;

public class LoadDeptInsuranceTransfer extends AbstractTransfer{
    public LoadDeptInsuranceTransfer(String method, Client client,String messageMethod) {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        Calendar calendar = Calendar.getInstance();
        Document tplDoc = getTplDoc(getMethod());
        Element dataRowElement = tplDoc.getRootElement().element("CommitData");
        return tplDoc.asXML();
    }
    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        if (executeStatus.equals("0")) {
            List<Element> dataRows = document.getRootElement().element("ReturnData").element("Data").elements();
            List<Map<String, Object>> dataList = new ArrayList<>();
            if (dataRows != null && dataRows.size() > 0) {
                for (Element dataRow : dataRows) {
                    Map<String, Object> dataMap = new HashMap<>();
                    LoadDeptInsuranceTransfer.LoadDeptInsuranceInnerMessage dataRowMessage = new LoadDeptInsuranceTransfer.LoadDeptInsuranceInnerMessage(dataRow);
                    dataMap.put("deptCode", dataRowMessage.getDeptCode());
                    dataMap.put("deptName", dataRowMessage.getDeptName());
                    dataMap.put("deptLocation", dataRowMessage.getDeptLocation());
                    dataMap.put("upperDeptCode", dataRowMessage.getUpperDeptCode());
                    dataMap.put("upperDeptName", dataRowMessage.getUpperDeptName());
                    dataMap.put("mtType", dataRowMessage.getMtType());
                    dataList.add(dataMap);
                }
            }
            resMap.put("list",dataList);
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
            return resMap;
        } else {
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);


        }
        return resMap;
    }

    private class LoadDeptInsuranceInnerMessage implements InnerMessage {
        /**
         * 科室代码
         */
        private String deptCode;
        /**
         * 科室名称
         */
        private String deptName;
        /**
         * 科室位置
         */
        private String deptLocation;
        /**
         * 上级科室代码
         */
        private String upperDeptCode;
        /**
         * 上级科室名称
         */
        private String upperDeptName;
        /**
         * 门特类型
         */
        private String mtType;


        public LoadDeptInsuranceInnerMessage(Element element){
            this.deptCode = element.attributeValue("DEPT_CODE");
            this.deptName = element.attributeValue("DEPT_NAME");
            this.deptLocation = element.attributeValue("DEPT_LOCATION");
            this.upperDeptCode = element.attributeValue("UPPER_DEPT_CODE");
            this.upperDeptName = element.attributeValue("UPPER_DEPT_NAME");
            this.mtType = element.attributeValue("MT_TYPES");
        }

        public String getDeptCode() { return deptCode; }

        public String getDeptName() { return deptName; }

        public String getDeptLocation() { return deptLocation; }

        public String getUpperDeptCode() { return upperDeptCode; }

        public String getUpperDeptName() { return upperDeptName; }

        public String getMtType() { return mtType; }

    }

}